
// const host = 'http://rap2.taobao.org:38080/app/mock/251796/'
const host = 'http://zwyzy.vipgz6.idcfengye.com/'
// const host = 'http://crms.ngrok2.xiaomiqiu.cn/'
// const host='http://3l114s5941.zicp.vip/'
// 同时发送异步代码的次数
let ajaxTimes = 0;

let headers = {'Content-Type':'application/json'}
function request (url, method, data) {
    ajaxTimes++;
  wx.showLoading({
    mask: true,
    title: '加载中' // 数据请求前loading
  })
  return new Promise((resolve, reject) => {
    let token =''
    token= wx.getStorageSync('token');
    console.log(token);
    if(token !== ''){
      headers = {
        'Content-Type':'application/json',
        'token':token
      }
    }
    console.log(headers);
    wx.request({
      url: host + url, 
      method: method,
      data: data,
      header:headers,
      success: function (res) {
        wx.hideLoading()
        resolve(res.data)
      },
      fail: function (res) {
        wx.hideLoading()
        wx.showToast({
          title: '请求失败请重试',
          icon:"none",
          duration: 1500
        });
        // reject(false)
      },
      complete: function () {
        ajaxTimes--;
        if(ajaxTimes===0){
            wx.hideLoading()
        }
      }
    })
    
  })
}
 
function get (obj) {
  return request(obj.url, 'GET', obj.data)
}
 
function post (obj) {
  return request(obj.url, 'POST', obj.data)
}
function put (obj) {
  return request(obj.url, 'PUT', obj.data)
}
export default {
  request,
  get,
  post,
  put,
  host
}